#include "FriendAndSessionListControl.h"
#include "Model/UIFriendModel.h"
#include "Model/UISessionModel.h"
#include <QDebug>
#include "Control/Init.h"
#include "IM_CoreApi.h"
#include <QQmlContext>
using namespace IM_GUI_KIT;
void FriendAndSessionListControl::deleteFriend(QString friendid,int type)
{
  if(type==1){
      for (int i = 0 ; i <myFriendLists->size();i++) {
          if(((UIFriendModel*)myFriendLists->at(i))->getSession()==friendid){
              myFriendLists->removeAt(i);
              // 更新列表
              Init::Instance()->getEngine()->rootContext()->setContextProperty("myFriendLists",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getFriendList()));
              break;
            }
        }
    }

}
#include <QQmlContext>

void FriendAndSessionListControl::deleteSession(QString sessionid,int type)
{
  if(type==1&&mySessionLists!=nullptr){
      for (int i = 0 ; i <mySessionLists->size();i++) {
          if(((UISessionModel*)mySessionLists->at(i))->getSession()==sessionid){
              mySessionLists->removeAt(i);
              // 更新列表
              Init::Instance()->getEngine()->rootContext()->setContextProperty("mySessionLists",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getSessionList()));
              if(IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->count(sessionid)>0){
                  delete IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->find(sessionid).value();
                  IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->remove(sessionid);
                }
              break;
            }
        }
    }

}

void FriendAndSessionListControl::createSession(QString sessionid,int type)
{
  // 测试
  if(IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->count(sessionid)>0){
      // 存在不创建
      qDebug()<<"session was start";
      (*mySessionLists)<<new UISessionModel(sessionid,"name","qrc:/other/icon/qqimg.jpg",1577299158202,"summary",1);
      // 排序 session
      qSort(mySessionLists->begin(),mySessionLists->end(),[](const QObject *l ,const QObject *r)->bool{
          if(((UISessionModel*)l)->getTimeStamp()>((UISessionModel*)r)->getTimeStamp()){
              return true;
            }
        });

      // 更新列表
      Init::Instance()->getEngine()->rootContext()->setContextProperty("mySessionLists",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getSessionList()));

      return;
    }else {
      qDebug()<<"add"<<sessionid;
      (*mySessionLists)<<new UISessionModel(sessionid,"name","qrc:/other/icon/qqimg.jpg",1577259158202,"summary",1);
      // 排序 session
      qSort(mySessionLists->begin(),mySessionLists->end(),[](const QObject *l ,const QObject *r)->bool{
          if(((UISessionModel*)l)->getTimeStamp()<((UISessionModel*)r)->getTimeStamp()){
              return true;
            }
        });

      // 更新列表
      Init::Instance()->getEngine()->rootContext()->setContextProperty("mySessionLists",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getSessionList()));

      // 同时获取会话数据
      if(messageMap->count(sessionid)>0){
          // 会话存在直接返回
          // 不判断类型
          Init::Instance()->getEngine()->rootContext()->setContextProperty("currSessionList",QVariant::fromValue(messageMap->find(sessionid).value()->getMessageList()));

        }else {
          //会话不存在则 创建map 再 拉取一波数据
          //通过 类型 和sessionid 获取 最新的消息 ,然后往回 读取  一部分列表
          MessageManager *manager=new MessageManager;
          //      manager
          manager->setSession(sessionid);
          messageMap->insert(sessionid,manager);

          // 测试
          UIMessageChatModel *data=new UIMessageChatModel(sessionid,"name","qrc:/other/icon/qqimg.jpg",1577259158202,"summary",1);
          manager->addMessageToList(data);


          // 更新
          Init::Instance()->getEngine()->rootContext()->setContextProperty("currSessionList",QVariant::fromValue(messageMap->find(sessionid).value()->getMessageList()));

        }
    }
}

FriendAndSessionListControl::FriendAndSessionListControl()
{
  qDebug()<<"init FriendAndSessionListControl";
  myFriendLists=new QList<QObject*>;
  mySessionLists=new QList<QObject*>;

  // 会话字典 存放的是 会话id 和 消息
  messageMap=new QMap<QString,MessageManager*>;

  // 测试
  (*myFriendLists)<<new UIFriendModel("123","name ","qrc:/other/icon/qqimg.jpg",1577259198202,"summary",1);
  (*mySessionLists)<<new UISessionModel("234","name ","qrc:/other/icon/qqimg.jpg",1577259198202,"summary",1);
}

FriendAndSessionListControl::~FriendAndSessionListControl()
{
  qDebug()<<"delete list";
  QObject *temp;
  if(myFriendLists!=nullptr){
      for (int i = 0 ; i < myFriendLists->size();i++) {
          myFriendLists->removeOne(temp);

          if(temp!=nullptr)delete temp;
          temp=nullptr;
        }
      myFriendLists->clear();
      delete myFriendLists;
    }
  if(mySessionLists!=nullptr){
      for (int i = 0 ; i < mySessionLists->size();i++) {
          mySessionLists->removeOne(temp);
          if(temp!=nullptr)delete temp;
          temp=nullptr;
        }
      mySessionLists->clear();
      delete mySessionLists;
    }

  if(messageMap!=nullptr){
      QMapIterator<QString,MessageManager*> imap(*messageMap);
      while (imap.hasNext()) {
          imap.next();
          delete imap.value();
        }
      messageMap->clear();
      delete messageMap;
    }
}

QList<QObject *> FriendAndSessionListControl::getFriendList()
{
  return *myFriendLists;
}

QList<QObject *> *FriendAndSessionListControl::getFriendListPort()
{
  return  myFriendLists;
}

QList<QObject *> FriendAndSessionListControl::getSessionList()
{
  return *mySessionLists;
}

QList<QObject *> *FriendAndSessionListControl::getSessionListPort()
{
  return mySessionLists;
}

QMap<QString,MessageManager*> *FriendAndSessionListControl::getMessageMapPort()
{
  return  messageMap;
}
